package com.lfd.scheduled.config;

import com.lfd.scheduled.job.MyJob;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.stereotype.Component;

/**
 * @项目名： spring-emq
 * @包名： com.lfd.scheduled.config
 * @文件名: QuartzConfig
 * @创建者: liufudong@szyh-smart.com
 * @创建时间: 2024/10/1 22:26
 * @描述：
 */
@Slf4j
@Component
public class QuartzConfig {
    @PostConstruct
    public void scheduleJob() {
        log.info("QuartzConfig scheduleJob");
        try {
            // 创建调度器
            StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();
            Scheduler scheduler = stdSchedulerFactory.getScheduler();
            scheduler.start();
            // 创建任务
            JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity("myJob", "group1").build();
            // 创建触发器
            CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1")
                    .withSchedule(CronScheduleBuilder.cronSchedule("0 36 22 * * ?")) // 每天 22:35 执行
                    .startNow().build();
            // 调度任务
            scheduler.scheduleJob(jobDetail, cronTrigger);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
